#define  _CRT_SECURE_NO_WARNINGS
#include <unordered_map>
using namespace std;

struct ListNode {
    int val;
    ListNode* next;
    ListNode() : val(0), next(nullptr) {}
    ListNode(int x) : val(x), next(nullptr) {}
    ListNode(int x, ListNode* next) : val(x), next(next) {}
};

class Solution {
public:
    ListNode* removeZeroSumSublists(ListNode* head) {
        ListNode* dummy = new ListNode(0);
        unordered_map<int, ListNode*> perfixSumMap;
        ListNode* cur = dummy;
        cur->next = head;
        int tmpsum = 0;
        while (cur)
        {
            tmpsum += cur->val;
            perfixSumMap[tmpsum] = cur;
            cur = cur->next;
        }

        tmpsum = 0;
        cur = dummy;
        while (cur)
        {
            tmpsum += cur->val;
            cur->next = perfixSumMap[tmpsum]->next;
            cur = cur->next;
        }
        return dummy->next;
    }
};